import { jsonp } from 'vue-jsonp';
// 腾讯地图密钥
export const MapKey = '3HMBZ-T6JK5-BMOIX-QZL4Y-XYURF-YOBSA';

// 地图poi搜索
export const getPoiSearch = (address, options) => {
  let { lat, lng, radius = 1000 } = options;
  lat = lat || this.center.lat;
  lng = lng || this.center.lng;
  let url = 'https://apis.map.qq.com/ws/place/v1/search';
  let params = {
    keyword: encodeURI(address),
    boundary: `nearby(${lat},${lng},${radius},1)`,
    output: 'jsonp',
    page_size: 20,
    key: MapKey
  }

  return new Promise((resolve, reject) => {
    jsonp(url, params).then(res => {
      if (res.status === 0) {
        resolve(res.data)
      } else {
        reject(res)
      }
    })
  })
}

// 地图路线规划
export const getRouteplan = (options, type = 'driving') => {
  const { from, to, waypoints } = options;
  let url = 'https://apis.map.qq.com/ws/direction/v1';
  switch (type) {
    case 'walking': // 步行
      url += '/walking';
      break;
    case 'transit': // 公交车
      url += '/transit';
      break;
    default: // 驾车
      url += '/driving';
  }
  let params = {
    from, // 起点 lat,lng
    to, // 终点 lat,lng
    output: 'jsonp',
    key: this.tk
  }
  if (waypoints) params.waypoints = waypoints;

  return new Promise((resolve, reject) => {
    jsonp(url, params).then(res => {
      if (res.status === 0) {
        resolve(res.result)
      } else {
        reject(res)
      }
    })
  })
}